from openrebooter4slotsremoved import *
from PyQt4 import QtCore, QtGui
import os

ONE_MINUTE = 1000 * 1 * 60 #  1 minute in milli-seconds


class Main(object):
	
	def __init__(self,obj):

    		self.sleeptime=0
		self.repeattime=0
		self.waitingtime=0
		self.obj=obj
		self.timer=QTimer()
    		
		self.counterfile=None
		
		
		
		QtCore.QObject.connect(obj.sleepSpinBox,QtCore.SIGNAL("valueChanged(int)"),self.defineSleepTime)
    		QtCore.QObject.connect(obj.repeatSpinBox,QtCore.SIGNAL("valueChanged(int)"),self.defineRepeatTime)
    		QtCore.QObject.connect(obj.waitingSpinBox,QtCore.SIGNAL("valueChanged(int)"),self.defineWaitingTime)
    		QtCore.QObject.connect(obj.pushButton,QtCore.SIGNAL("clicked()"),self.Run)
		QtCore.QObject.connect(self.timer,SIGNAL("timeout()"),self.reRun)
		
		
    	def defineSleepTime(self, sleeptime):
		self.sleeptime=sleeptime
		#print "defineSleepTime run"
	
	
    	def defineRepeatTime(self, repeattime):
		self.repeattime=repeattime
		self.obj.lcdRepeatNumber.display(self.repeattime)
		#print "defineRepeatTime run"
		
	def defineWaitingTime(self, waittime):
		self.waittime=waittime
		qtime=QtCore.QTime(0,self.waittime)
		self.obj.leftTimeEdit.setTime(qtime)
		
	def self.createFile(self):
		self.counterfile=file("counternum.txt","w+")
		
	def getFileCounter(self):
		counter=self.counterfile.readline().strip()
		return int(counter)
	
	def updateCounterFile(self,counter):
		self.counterfile.seek(0,0)
		self.counterfile.writeline(str(counter))
		self.counterfile.flush()
	
	def displayPass(self):
		QtGui.QMessageBox.information(self.obj,"PASS")
		self.timer.stop()
		
	def decreaseCounters(self):
		filecounter=self.getFileCounter()
		counter=filecounter-1
		self.updateCounterFile(counter)
		self.obj.lcdRepeatNumber.display(counter)
		
	def addLogMessage(self):
		x=datetime.datetime..now()
		message1=":".join(map(lampda x: str(x), [x.day,x.month,x.year]))
		message2=":".join(map(lampda x: str(x), [x.hour,x.minute,x.second]))
		message=message1+message2
		self.obj.addItem(message)	
		
	def checkFile():
			
		if self.filecounter != None: return True
		else: return False
		
	def reRun(self):
		fcounter=self.getFileCounter()	
		if.fcounter != self.repeattime:
			self.Run()
		else:
			self.displayPass()
		
	def Run(self):
		#print "do it"
		#add the script as python something to /etc/init.d/rc.local
		
		if self.checkFile():
			self.decreaseCounters()
		else:		
			self.createTheFile()
			WAIT_TIME=self.waittime*1000*1 # num of seconds to wait
			TIMER_MINUTE=ONE_MINUTE+self.sleeptime+WAIT_TIME
			self.timer.start(TIMER_MINUTE, FALSE)
			
		self.addLogMessage()
		os.system(sleepCommand)
		
		
		#if the counter reaches the end show PASS screen adn remove the rc.local line
		#add the date log to the listbox
		

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    
    m=Main(ui)
    
    MainWindow.show()
    sys.exit(app.exec_())